package com.burgeon.backend.Rest.coupon.bos;
/**
 * 获取所有优惠券
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.Coupon;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class GetCouponALLBos extends RESTBase {
    private final static int RANGE = 50;//分页数量

    private String ip;
    private String account;
    private String password;

    public GetCouponALLBos(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */

    public HashMap getData(int start) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(start), url);

        HashMap rows = new HashMap();
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)){
            throw new Exception("接口获取数据失败");
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");

        rows.put("count", count);
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Coupon> list = new ArrayList<Coupon>();

        for (int i = 0; i < jsonArray.length(); i++) {
            Coupon coupon = new Coupon();
            coupon.setId(jsonArray.getJSONArray(i).getString(0));
            coupon.setCode(jsonArray.getJSONArray(i).getString(1));
            coupon.setType(jsonArray.getJSONArray(i).getString(2));
            coupon.setCheckno(jsonArray.getJSONArray(i).getString(3));
            coupon.setMoney(jsonArray.getJSONArray(i).getString(4));
            coupon.setDatebeg(jsonArray.getJSONArray(i).getString(5));
            coupon.setDateend(jsonArray.getJSONArray(i).getString(6));
            coupon.setVerifyed(jsonArray.getJSONArray(i).getString(7));
            coupon.setValided(jsonArray.getJSONArray(i).getString(8));
            coupon.setCreation(jsonArray.getJSONArray(i).getString(9));
            coupon.setVipCode(jsonArray.getJSONArray(i).getString(10));
            coupon.setOriginal(jsonArray.getJSONArray(i).getString(11));
            coupon.setDiscount(jsonArray.getJSONArray(i).getString(12));
            list.add(coupon);
        }
        rows.put("data", list);

        return rows;
    }

    private MultiValueMap Data(int y) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段
        columns.add("ID");
        columns.add("VOUCHERS_NO");//券号
        columns.add("VOU_TYPE");//类型
        columns.add("IDENTIFY_CODE");//检验码
        columns.add("AMT_DISCOUNT");//面值
        columns.add("START_DATE");//开始时间
        columns.add("VALID_DATE");//结束时间
        columns.add("IS_VERIFYED");//核销标记
        columns.add("IS_VALID");//有效标记
        columns.add("CREATIONDATE");//创建时间
        columns.add("'C_VIP_ID;CARDNO'");//vip卡号
        columns.add("AMT_NOLES");//原价不小于
        columns.add("VOU_DIS");//折扣

        params.put("columns", columns);
        params.put("table", "C_V_VOUCHERS");//表名

        params.put("range", RANGE);
        params.put("count", "true");

        params.put("start", String.valueOf(y * RANGE));
        HashMap orderby = new HashMap();

        orderby.put("column", "CREATIONDATE");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);
        HashMap paramss = new HashMap();

        paramss.put("column", "U_SCENARIOS");
        paramss.put("condition", "3");
        params.put("params", paramss);

        params.put("orderby", list);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GetCouponALLBos g = new GetCouponALLBos("39.99.239.133:55322", "nea@burgeon.com.cn", "mgg55321");
        try {
            System.err.println(g.getData(0));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
